package com.dyjch.mly.controller;


import com.dyjch.mly.entity.GoodsBound;
import com.dyjch.mly.mapper.GoodsOutBoundMapper;
import com.dyjch.mly.service.imp.GoodsManagerServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Date;
import java.util.List;

@Controller
public class GoodsOutBoundController {

    @Autowired
    GoodsOutBoundMapper goodsOutBoundMapper;

    @Autowired
    GoodsManagerServiceImp goodsManagerService;

    @RequestMapping("/outbound.html")
    public String list(Model model){
        List<GoodsBound> goods = goodsOutBoundMapper.getAllMessage();
        model.addAttribute("mess",goods);
        return "outbound";
    }

    @PostMapping("/GoodsOutBoundOperation")
    public String GoodsOutBound(String name,String type,int number){
        GoodsBound goodsBound = goodsManagerService.queryGoodsByName(name);
        Date date=new Date(new java.util.Date().getTime());
        if (goodsBound.getNumber()<number){
            return "redirect:outbound.html";
        } else if (goodsBound.getNumber()==number){
            goodsManagerService.deletGoodsByName(name);
            goodsOutBoundMapper.addGoodsOutBoundMessage(new GoodsBound(0,name,type,number,date));
        } else {
            goodsManagerService.updateWithNumber(name,-number);
            goodsOutBoundMapper.addGoodsOutBoundMessage(new GoodsBound(0,name,type,number,date));
        }

        return "redirect:outbound.html";
    }

}
